import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

//维修资质
class MaintainQualification extends StatefulWidget {
  final List<File> fileList;
  final Function getImage;

  const MaintainQualification({Key key, this.fileList, this.getImage})
      : super(key: key);

  @override
  _MaintainQualificationState createState() => _MaintainQualificationState();
}

class _MaintainQualificationState extends State<MaintainQualification> {
  int maxLength = 8; //图片最大数量
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          child: Text(
            "维修资质",
            style: TextStyle(
                color: YTTheme.theme,
                fontSize: setSp(15),
                fontWeight: FontWeight.w600),
          ),
        ),
        SizedBox(
          height: setWidth(46),
        ),
        //维修资质
        Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Text(
                  "维修资质",
                  style:
                      TextStyle(color: Color(0xff321212), fontSize: setSp(15)),
                ),
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {},
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      child: Text(
                        "一级维修资质",
                        style: TextStyle(
                            color: Color(0xff323232), fontSize: setSp(15)),
                      ),
                    ),
                    SizedBox(
                      width: setWidth(9),
                    ),
                    Container(
                      width: setWidth(6),
                      height: setWidth(11),
                      child: Image.asset(
                        "assets/images/right.png",
                        fit: BoxFit.cover,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(40),
        ),
        //图片列表
        Container(
          child: StaggeredGridView.countBuilder(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            crossAxisCount: 3,
            mainAxisSpacing: setWidth(10),
            crossAxisSpacing: setWidth(10),
            staggeredTileBuilder: (i) {
              return StaggeredTile.fit(1);
            },
            itemCount: widget.fileList.length + 1,
            itemBuilder: (context, index) {
              return GestureDetector(onTap: () {}, child: _gridItem(index));
            },
          ),
        )
      ],
    );
  }

  Widget _gridItem(int index) {
    if (index + 1 > maxLength) {
      return Container();
    }
    //点击选择图片
    if (widget.fileList.length == 0 || index == widget.fileList.length) {
      return Row(
        children: [
          GestureDetector(
            onTap: () {
              widget.getImage();
            },
            child: Container(
              width: setWidth(100),
              height: setWidth(100),
              child: SizedBox(
                child:
                    Image.asset("assets/images/addpic.png", fit: BoxFit.cover),
              ),
            ),
          )
        ],
      );
    }

    //已选取图片
    return Row(
      children: [
        GestureDetector(
          onTap: () {
            widget.getImage();
          },
          child: Container(
            width: setWidth(100),
            height: setWidth(100),
            child: Image.file(
              widget.fileList[index],
              fit: BoxFit.cover,
            ),
          ),
        )
      ],
    );
  }
}
